using System;
using System.Collections.Generic;
using CMS_Admin.Backend.Api.Entity;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using CMS_Admin.Backend.Api.ParamModel;
using Newtonsoft.Json;
using CMS_Admin.Backend.Api.Repository;
using CMS_Admin.Backend.Api.Utils;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Authorization;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;

namespace CMS_Admin.Backend.Controllers
{


    /// <summary>
    /// 图片上传
    /// </summary>

    [ApiController]
    [Route("upload")]
    public class PictureController : ControllerBase
    {
        private readonly IRepository<Resource> _resource;

        public PictureController(IRepository<Resource> resource)
        {
            _resource = resource;
        }


        /// <summary>
        /// 图片上传
        /// </summary>
        /// <returns></returns>
        [HttpPost("picture")]
        public async Task<ActionResult> UploadFileAsync()
        {


            string filesPath = "";
            var files = Request.Form.Files;
            string tempPath = DateTime.Now.ToString("yyyyMMdd");//相对路径
            string physicPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "uploadfiles");//物理路径
            if (!Directory.Exists(physicPath))
            {
                Directory.CreateDirectory(physicPath);
            }
            foreach (var formFile in files)
            {
                if (formFile.Length > 0)
                {
                    //文件名字
                    string filesName = DateTime.Now.ToFileTime() + Path.GetExtension(formFile.FileName);

                    //绝对路径
                    string fileFullPath = Path.Combine(physicPath, filesName);
                    using (var stream = new FileStream(fileFullPath, FileMode.Create))
                    {
                        await formFile.CopyToAsync(stream);//保存文件


                        
                    }
                    filesPath += "http://" + Request.Host.Value + "/uploadfiles/" + filesName;

                    await _resource.InsertAsync(new Resource{
                        ResoutceName=filesName
                    });
                }
            }
            // filesPath = filesPath.TrimEnd(',');
            return Ok(new
            {
                Code = 200,
                Data = new
                {
                    Url = filesPath,
                    Msg = "上传成功"
                }
            });
        }

        /// <summary>
        /// 删除图片
        /// </summary>
        /// <returns></returns>
        [HttpDelete("{pid}")]
        public ActionResult RemoveFileAsync(string pid)
        {

            string physicPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "uploadfiles");//物理路径
            string filePath = physicPath + "/" + pid;
            if (System.IO.File.Exists(filePath))
            {
                System.IO.File.Delete(filePath);
                var rc=_resource.Table.Where(r =>r.ResoutceName==pid).FirstOrDefault();
                rc.ResoutceName="https://img1.baidu.com/it/u=3886212450,854269223&fm=26&fmt=auto&gp=0.jpg";
                _resource.Update(rc);

                return Ok(new {
                    Code=200,
                    Data =new {
                        Msg ="删除成功"
                    }
                });
            }

                return Ok(new {
                    Code=44440,
                    Data =new {
                        Msg ="删除失败"
                    }
                });
            
        }

    }


}